package com.xbongbong.paas.help.analysis.data;

import com.alibaba.fastjson.JSONObject;
import com.xbongbong.paas.domain.entity.ext.PaasAttachmentEntityExt;
import com.xbongbong.paas.enums.FieldTypeEnum;
import com.xbongbong.paas.field.FieldAttrEntity;
import com.xbongbong.paas.help.UserHelper;
import com.xbongbong.paas.pojo.dto.AnalysisDataDTO;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.BeanUtil;
import com.xbongbong.paas.toolbox.util.StringUtil;
import com.xbongbong.parent.abstracts.AbstractFormDataAnalysisDataHelp;
import com.xbongbong.pro.constant.BusinessConstant;
import com.xbongbong.pro.user.pojo.ReturnUserAndDepartmentPojo;
import com.xbongbong.saas.constant.BasicConstant;
import com.xbongbong.saas.enums.SelectProductEnum;
import com.xbongbong.saas.enums.business.ContactEnum;
import com.xbongbong.saas.enums.business.ProductEnum;
import com.xbongbong.saas.help.FormDataAnalysisHelp;
import com.xbongbong.saas.service.toolbox.help.AttrDecimalPrecisionHelper;
import com.xbongbong.saas.util.SpecificationUtil;
import com.xbongbong.sys.domain.entity.DepartmentEntity;
import com.xbongbong.sys.domain.entity.UserEntity;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;

/**
 * 获取数据关联查询、关联数据时解析数据
 * @author: wufeng
 * @date: 2018/12/11 15:17
 * @desrcption:
 */
@Component
public class FormDataLinkSearchAnalysisDataHelp extends AbstractFormDataAnalysisDataHelp {

    @Resource
    private FormDataAnalysisHelp formDataAnalysisHelp;
    @Resource
    private AttrDecimalPrecisionHelper attrDecimalPrecisionHelper;

    @Override
    public void analysisData(AnalysisDataDTO analysisDataDTO, JSONObject resultJsonObject, Map<String, Object> linkData, Set<Object> linkDataSet) throws XbbException {
        FieldAttrEntity fieldAttrEntity = analysisDataDTO.getFieldAttrEntity();
        String key = analysisDataDTO.getAttr();
        JSONObject objectJsonObject = analysisDataDTO.getDataJsonObject();
        Map<String, UserEntity> userMap = analysisDataDTO.getUserMap();
        Map<String, DepartmentEntity> departmentMap = analysisDataDTO.getDepartmentMap();
        // TODO 关于成员和部门回显需要去掉离职人员，现在直接不限，具体如何显示，未解决
         if(FieldTypeEnum.RADIO_GROUP.getType().equals(fieldAttrEntity.getFieldType())
                        || FieldTypeEnum.COMBO.getType().equals(fieldAttrEntity.getFieldType())) {
             if (Objects.equals(fieldAttrEntity.getSaasAttr(), SelectProductEnum.UNIT.getSaasAttr()) || Objects.equals(fieldAttrEntity.getSaasAttr(), ProductEnum.UNIT.getSaasAttr())) {
                 // 产品单位特殊处理下
                 formDataAnalysisHelp.analysisCombo(objectJsonObject, resultJsonObject,fieldAttrEntity, analysisDataDTO.getCorpid());
             } else {
                 //单选、下拉单选返回带颜色的ItemPoJO
                 formDataAnalysisHelp.analysisComboWithColor(objectJsonObject, resultJsonObject,fieldAttrEntity, analysisDataDTO.getCorpid());
             }
         } else if (Objects.equals(FieldTypeEnum.STAGE_THRUSTER.getType(), fieldAttrEntity.getFieldType())) {
            formDataAnalysisHelp.analysisListStageJsonObjectWithColor(resultJsonObject, analysisDataDTO);
         } else if((FieldTypeEnum.CHECKBOX_GROUP.getType().equals(fieldAttrEntity.getFieldType())
                 || FieldTypeEnum.COMBO_CHECK.getType().equals(fieldAttrEntity.getFieldType()))) {
             formDataAnalysisHelp.analysisComboReturnStrWithColor(objectJsonObject, resultJsonObject,fieldAttrEntity);
         } else if(FieldTypeEnum.USER.getType().equals(fieldAttrEntity.getFieldType())){
            if(StringUtil.isNotEmpty(objectJsonObject.getString(key))){
                UserEntity userEntity = userMap.get(objectJsonObject.getString(key));
                if (Objects.nonNull(userEntity)) {
                    ReturnUserAndDepartmentPojo returnUserAndDepartmentPojo = new ReturnUserAndDepartmentPojo();
                    BeanUtil.copyProperties(userEntity, returnUserAndDepartmentPojo);
                    returnUserAndDepartmentPojo.setId(userEntity.getUserId());
                    resultJsonObject.put(key, returnUserAndDepartmentPojo);
                }
            }
        } else if (FieldTypeEnum.USER_GROUP.getType().equals(fieldAttrEntity.getFieldType())) {
            List<UserEntity> userList = UserHelper.getUserByKey(key, objectJsonObject, userMap);
            List<ReturnUserAndDepartmentPojo> resultList = new ArrayList<>();
            userList.forEach(item->{
                ReturnUserAndDepartmentPojo returnUserAndDepartmentPojo = new ReturnUserAndDepartmentPojo();
                BeanUtil.copyProperties(item, returnUserAndDepartmentPojo);
                returnUserAndDepartmentPojo.setId(item.getUserId());
                resultList.add(returnUserAndDepartmentPojo);
            });
            resultJsonObject.put(key, resultList);
        } else if(FieldTypeEnum.DEPT.getType().equals(fieldAttrEntity.getFieldType())){
            DepartmentEntity departmentEntity = new DepartmentEntity();
            if(StringUtil.isNotEmpty(objectJsonObject.getString(key))){
                departmentEntity =  departmentMap.get(objectJsonObject.getString(key));
            }
            resultJsonObject.put(key, departmentEntity);
        } else if (FieldTypeEnum.DEPT_GROUP.getType().equals(fieldAttrEntity.getFieldType())) {
            List<DepartmentEntity> departmentList = UserHelper.getDepartmentByKey(key, objectJsonObject, departmentMap);
            resultJsonObject.put(key, departmentList);
        } else if (FieldTypeEnum.ADDRESS.getType().equals(fieldAttrEntity.getFieldType())) {
             JSONObject addressJsonObject = objectJsonObject.getJSONObject(key);
             if (Objects.nonNull(addressJsonObject) && Objects.equals(fieldAttrEntity.getShowDetailAddress(), 0)) {
                 addressJsonObject.put(BusinessConstant.ADDRESS, "");
             }
             String addressData = "";
             if (Objects.nonNull(addressJsonObject)) {
                 String province = addressJsonObject.getString(BusinessConstant.PROVINCE);
                 String city = addressJsonObject.getString(BusinessConstant.CITY);
                 String district = addressJsonObject.getString(BusinessConstant.DISTRICT);
                 String address = addressJsonObject.getString(BusinessConstant.ADDRESS);
                 addressData = province + city + district + address;
             }
             resultJsonObject.put(key, addressData);
         } else if (FieldTypeEnum.UPLOAD.getType().equals(fieldAttrEntity.getFieldType())) {
             List<PaasAttachmentEntityExt> attachmentStrList = formDataAnalysisHelp.analysisAttachmentList(objectJsonObject.getString(key));
             resultJsonObject.put(key, attachmentStrList);
         } else if (FieldTypeEnum.LINK_DATA.getType().equals(fieldAttrEntity.getFieldType()) && Objects.nonNull(objectJsonObject.get(key))
                 && objectJsonObject.get(key) instanceof Number && !Objects.equals(objectJsonObject.get(key), 0)){
            linkData.put(key, objectJsonObject.get(key));
            if(objectJsonObject.get(key) != null){
                linkDataSet.add(objectJsonObject.get(key));
            }
            resultJsonObject.put(key, objectJsonObject.get(key));
        } else if(Objects.equals(fieldAttrEntity.getSaasAttr(), ContactEnum.PHONE.getSaasAttr())) {
             formDataAnalysisHelp.analysisTelephoneNumber(fieldAttrEntity, objectJsonObject, resultJsonObject, analysisDataDTO.getLoginUser());
         } else if (Objects.equals(FieldTypeEnum.NUM.getType(), fieldAttrEntity.getFieldType())) {
             // 小数精度
             boolean visible = true;
             double value = 0D;
             try {
                 if (Objects.equals(objectJsonObject.getString(key), BasicConstant.NULL_SHOW_STR)) {
                     visible = false;
                 } else {
                     value = objectJsonObject.getDoubleValue(key);
                 }
             } catch (Exception e) {
                 value = 0D;
             }
             if (visible) {
                 String numberType = fieldAttrEntity.getNumberType();
                 boolean rateNumFlag = FieldAttrEntity.isRate(fieldAttrEntity.getSaasAttr(), numberType);
                 if (!rateNumFlag) {
                     resultJsonObject.put(key, attrDecimalPrecisionHelper.formatPrecisionWithIntegerOnly(value, fieldAttrEntity));
                 } else {
                     String decPrecision = attrDecimalPrecisionHelper.setDecPrecision(value * 100, fieldAttrEntity.getAccuracy());
                     resultJsonObject.put(key, decPrecision + "%");
                 }
             }
         } else {
            resultJsonObject.put(key, objectJsonObject.get(key));
             SpecificationUtil.getProductName(analysisDataDTO.getBusinessType(), key, objectJsonObject, resultJsonObject);

        }
    }
}
